package no.uials.distsys;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Map;
import no.uials.distsys.User.Role;

/**
 *
 * @author Anders
 */
public class DistSysSystem extends UnicastRemoteObject implements DistSysService {
    private Warehouse warehouse;
    private Map<String, User> users;
    
    /**
     * 
     * @param warehouse
     * @throws RemoteException 
     */
    public DistSysSystem(Warehouse warehouse) throws RemoteException {
        this.warehouse = warehouse;
        this.users = warehouse.getUsers();
    }

    /**
     * 
     * getUserSystem()
     * 
     * @param uid
     * @param pwd
     * @return
     * @throws RemoteException
     * @throws DistSysException 
     */
    @Override
    public UserService getUserSystem(String uid, String pwd) throws RemoteException, DistSysException {
        User user = null;
        if (!users.containsKey(uid)) {
            throw new DistSysException("No user with ID " + uid + " exists in the system.");
        } else {
            user = users.get(uid);
        }
        
        if (!user.isUserInRole(User.Role.USER)) {
            throw new DistSysException();
        }
        
        if (!user.validatePw(pwd)) {
            throw new DistSysException("Incorrect password. Try again.");
        }
        
        return new WarehouseSystem(warehouse);
    }

    /**
     * 
     * getAdminSystem()
     * 
     * @param uid
     * @param pwd
     * @return
     * @throws RemoteException
     * @throws DistSysException 
     */
    @Override
    public AdminService getAdminSystem(String uid, String pwd) throws RemoteException, DistSysException {
        User user = null;
        if (!users.containsKey(uid)) {
            throw new DistSysException("No user with ID " + uid + " exists in the system.");
        } else {
            user = users.get(uid);
        }
        
        if (!user.isUserInRole(User.Role.ADMIN)) {
            throw new DistSysException();
        }
        
        if (!user.validatePw(pwd)) {
            throw new DistSysException("Incorrect password. Try again.");
        }
        
        return new WarehouseSystem(warehouse);
    }

    /**
     * 
     * addUser()
     * 
     * @param uid
     * @param pwd
     * @param role
     * @return
     * @throws RemoteException
     * @throws DistSysException 
     */
    @Override
    public User addUser(String uid, String pwd, Role role) throws RemoteException, DistSysException {
        return addUser(new User(pwd, pwd, role));
    }

    /**
     * 
     * addUser()
     * 
     * @param user
     * @return
     * @throws RemoteException
     * @throws DistSysException 
     */
    @Override
    public User addUser(User user) throws RemoteException, DistSysException {
        return warehouse.addUser(user);
    }

    /**
     * 
     * addArticle()
     * 
     * @param name
     * @param price
     * @param stock
     * @param aisle
     * @param section
     * @param shelf
     * @return
     * @throws RemoteException
     * @throws DistSysException 
     */
    @Override
    public Article addArticle(String name, Double price, int stock, int aisle, int section, int shelf) throws RemoteException, DistSysException {
        return warehouse.addArticle(name, price, stock, aisle, section, shelf);
    }

    /**
     * 
     * addArticle()
     * 
     * @param name
     * @param price
     * @param stock
     * @param pos
     * @return
     * @throws RemoteException
     * @throws DistSysException 
     */
    @Override
    public Article addArticle(String name, Double price, int stock, Position pos) throws RemoteException, DistSysException {
        return warehouse.addArticle(name, price, stock, pos);
    }
}